package com.lysb.core.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * base64
 *
 * @author yangqihang
 */
public class Base64Utils {

    public static String encodeToString(byte[] content) {
        if (content == null) {
            return StringUtils.EMPTY;
        }
        return new String(Base64.encodeBase64(content));
    }

    public static String decodeToString(byte[] content) {
        if (content == null) {
            return StringUtils.EMPTY;
        }
        return new String(Base64.decodeBase64(content));
    }

    public static String encodeToString(String content) {
        if (StringUtils.isBlank(content)) {
            return StringUtils.EMPTY;
        }
        return new String(Base64.encodeBase64(content.getBytes()));
    }

    public static String decodeToString(String content) {
        if (StringUtils.isBlank(content)) {
            return StringUtils.EMPTY;
        }
        return new String(Base64.decodeBase64(content.getBytes()));
    }

    public static byte[] encode(byte[] content) {
        if (content == null) {
            return ArrayUtils.EMPTY_BYTE_ARRAY;
        }
        return Base64.encodeBase64(content);
    }

    public static byte[] decode(byte[] content) {
        if (content == null) {
            return ArrayUtils.EMPTY_BYTE_ARRAY;
        }
        return Base64.decodeBase64(content);
    }

    public static byte[] encode(String content) {
        if (StringUtils.isBlank(content)) {
            return ArrayUtils.EMPTY_BYTE_ARRAY;
        }
        return Base64.encodeBase64(content.getBytes());
    }

    public static byte[] decode(String content) {
        if (StringUtils.isBlank(content)) {
            return ArrayUtils.EMPTY_BYTE_ARRAY;
        }
        return Base64.decodeBase64(content.getBytes());
    }

}
